<?php
namespace libs\qoy\Val\Formatter;
/**
 * ValFormatterCallable 格式化回调类型
 */

use libs\qoy\Val\IValFormatter;
use libs\qoy\Exception\ExceptionVerify;
class FormatterCallable extends FormatterBase implements IValFormatter {

    /**
     * 对回调类型进行格式化
     *
     * @param mixed $value 变量值
     * @param array $rule array('callback' => '回调函数', 'params' => '第三个参数')
     * @return boolean/string 格式化后的变量
     *
     */
    public function parse($value, $rule) {
        if (!isset($rule['callback']) || !is_callable($rule['callback'])) {
            
            throw new ExceptionVerify(ExceptionVerify::INVALID_CALLBACK_RULE,array('name' => $rule['name']));
//            throw new Exception_InternalServerError(
//                T('invalid callback for rule: {name}', array('name' => $rule['name']))
//            );
        }

        if (isset($rule['params'])) {
            return call_user_func($rule['callback'], $value, $rule, $rule['params']);
        } else {
            return call_user_func($rule['callback'], $value, $rule);
        }
    }
}
